﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace MusicXML
{
    public partial class FRM_Main : Form
    {
        bool isOpen = false;
        Image clef = MusicXML.Properties.Resources.clef;
        Image whole = MusicXML.Properties.Resources.whole;
        Image eighth = MusicXML.Properties.Resources.eighth;
        Image half = MusicXML.Properties.Resources.half;
        Image quarter = MusicXML.Properties.Resources.quarter;
        Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
        int i, j;

        public FRM_Main()
        {
            InitializeComponent();
        }

        private void FRM_Main_Load(object sender, EventArgs e)
        {
            TB_Title.TextAlign = HorizontalAlignment.Center;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "MusicXML Files | *.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MusicData.LoadFile(openFileDialog1.FileName);
                isOpen = true;
                TB_Title.Text = MusicData.GetTitle();
                this.Panel_Sheet.Refresh();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "MusicXML Files | *.xml";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MusicData.SaveFile(saveFileDialog1.FileName);
            }
        }

        private int DrawLine(PaintEventArgs e)
        {
            int dem = MusicData.PartData.GetElementsByTagName("note").Count;
            if (dem % 20 > 0)
                dem = dem / 20 + 1;
            else
                dem = dem / 20;
            dem = dem * 5 + 1;
            int l_h = 30;
            for (i = 1; i < dem; i++)
            {
                e.Graphics.DrawLine(pen, 10, l_h, Panel_Sheet.Width - 10, l_h);
                if (i % 5 == 0)
                    {
                        e.Graphics.DrawImage(clef, 20, l_h - 45, 21, 60);
                        l_h += 50;
                    }
                else
                {
                    l_h += 8;
                }
            }
            return l_h;
        }

        private int GetPosY(string step)
        {
            switch (step)
            {
                case "E": return -3; break;
                case "F": return -7; break;
                case "G": return -11; break;
                case "A": return -15; break;
                case "B": return -19; break;
                case "C": return 5; break;
                case "D": return 1; break;
                default: return 0; break;
            }
        }

        private void DrawMusicNote(PaintEventArgs e)
        {
            XmlDocument Temp = new XmlDocument();
            XmlNodeList Notes = MusicData.PartData.GetElementsByTagName("note");
            int width = 70;
            int height = 62;
            int h = 0;
            int octave;
            for (i = 0; i < Notes.Count; i++)
            {
                string xmlstring = "<root>" + Notes[i].InnerXml + "</root>";
                Temp.LoadXml(xmlstring);
                if (Temp.GetElementsByTagName("step").Count > 0)
                {
                    h = GetPosY(Temp.GetElementsByTagName("step")[0].InnerText);
                    octave = Convert.ToInt32(Temp.GetElementsByTagName("octave")[0].InnerText);
                    h = h - ((octave - 4) * 28);
                    if (Temp.GetElementsByTagName("type")[0].InnerText == "whole")
                    {
                        e.Graphics.DrawImage(whole, width, height + h, 13, 8);
                    }
                    else if (Temp.GetElementsByTagName("type")[0].InnerText == "quarter")
                    {
                        e.Graphics.DrawImage(quarter, width, height + h - 24, 10, 32);
                    }
                    else if (Temp.GetElementsByTagName("type")[0].InnerText == "half")
                    {
                        e.Graphics.DrawImage(half, width, height + h - 24, 10, 32);
                    }
                    else if (Temp.GetElementsByTagName("type")[0].InnerText == "eighth")
                    {
                        e.Graphics.DrawImage(eighth, width, height + h - 24, 20, 32);
                    }

                    if ((i + 1) % 20 == 0)
                    {
                        height += 82;
                        width = 70;
                    }
                    else
                        width += 33;
                }
            }
        }

        private void Panel_Sheet_Paint(object sender, PaintEventArgs e)
        {
            if (isOpen)
            {
                Panel_Sheet.Height = DrawLine(e);
                DrawMusicNote(e);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
